#include "SerialBtn.h"
#include <QDir>
#include <QDebug>
#include <QDataStream>
#include <QFile>


SerialBtn::SerialBtn(QObject *parent) : QObject(parent)
{
    QFile fileConf("./serial_config.txt");
    if (!fileConf.open(QIODevice::ReadOnly))
    {
        qDebug() << "open serial_config.txt failed";
        return;
    }

    QString strPortName = QString(fileConf.readAll());
    qDebug() << "strPortName: " << strPortName;

    m_serialPort = new QSerialPort(strPortName);
    m_serialPort->setBaudRate(QSerialPort::Baud9600);
    m_serialPort->setParity(QSerialPort::NoParity);
    m_serialPort->setDataBits(QSerialPort::Data8);
    m_serialPort->setStopBits(QSerialPort::OneStop);
    m_serialPort->open(QIODevice::ReadWrite);
    connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(onReadReady()));
}

void SerialBtn::onReadReady()
{
    QString strRecv = QString(m_serialPort->readAll());
    if (strRecv == "po")
    {
        m_serialPort->write(QString("pos").toLocal8Bit());
        emit sigPowerOn();
    }
    else if (strRecv == "st")
    {
        m_serialPort->write(QString("sts").toLocal8Bit());
        emit sigStart();
    }
}




